import datetime

from exts import db


class Address(db.Model):
    __tablename__ = 'address'
    id = db.Column(db.Integer, primary_key=True, nullable=False, autoincrement=True)
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)
    # user = db.relationship("Users", backref="address")
    name = db.Column(db.String(64), nullable=False)
    province = db.Column(db.String(64), nullable=False)
    city = db.Column(db.String(64), nullable=False)
    county = db.Column(db.String(64), nullable=False)
    address = db.Column(db.String(255), nullable=False)
    phone = db.Column(db.String(64), nullable=False)
    is_default = db.Column(db.Boolean, default=False)
    created_at = db.Column(db.DateTime, nullable=False)
    updated_at = db.Column(db.DateTime, default=datetime.datetime.now(), nullable=False)


class Cart(db.Model):
    __tablename__ = 'cart'
    id = db.Column(db.Integer, primary_key=True, nullable=False, autoincrement=True)
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)
    # user = db.relationship("Users", backref="cart")
    goods_json = db.Column(db.Text, nullable=False)
    created_at = db.Column(db.DateTime, nullable=False)
    updated_at = db.Column(db.DateTime, default=datetime.datetime.now(), nullable=False)


class Categories(db.Model):
    __tablename__ = 'categories'
    id = db.Column(db.Integer, primary_key=True, nullable=False, autoincrement=True)
    name = db.Column(db.String(64), nullable=False)
    pid = db.Column(db.Integer, default=0)
    status = db.Column(db.Integer, default=1)
    level = db.Column(db.Integer, default=1)
    created_at = db.Column(db.DateTime, nullable=False)
    updated_at = db.Column(db.DateTime, default=datetime.datetime.now(), nullable=False)


class Comments(db.Model):
    __tablename__ = 'comments'
    id = db.Column(db.Integer, primary_key=True, nullable=False, autoincrement=True)
    user_id = db.Column(db.Integer, nullable=False)
    # user = db.relationship("Users", db.ForeignKey("users.id"), backref="comments")
    order_id = db.Column(db.Integer, nullable=False)
    # order = db.relationship("Orders", db.ForeignKey("orders.id"), backref="comments")
    goods_id = db.Column(db.Integer, nullable=False)
    # goods = db.relationship("Goods", db.ForeignKey("goods.id"), backref="comments")
    content = db.Column(db.Text, nullable=False)
    rate = db.Column(db.Integer, default=1)
    star = db.Column(db.Integer, default=5)
    reply = db.Column(db.Text)
    pics = db.Column(db.Text)
    created_at = db.Column(db.DateTime, nullable=False)
    updated_at = db.Column(db.DateTime, default=datetime.datetime.now(), nullable=False)


class Goods(db.Model):
    __tablename__ = 'goods'
    id = db.Column(db.Integer, primary_key=True, nullable=False, autoincrement=True)
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)
    # user = db.relationship("Users", backref="goods")
    category_id = db.Column(db.Integer, db.ForeignKey("categories.id"), nullable=False)
    # category = db.relationship(Categories, backref="goods")
    title = db.Column(db.String(64), nullable=False)
    description = db.Column(db.String(255), nullable=False)
    price = db.Column(db.DECIMAL(11, 2), nullable=False)
    stock = db.Column(db.Integer, nullable=False)
    cover = db.Column(db.String(255), nullable=False, default='undefined')
    pics = db.Column(db.Text, nullable=False, default='{}')
    details = db.Column(db.Text, nullable=False)
    sales = db.Column(db.Integer, default=0)
    is_on = db.Column(db.Boolean, default=False)
    is_recommend = db.Column(db.Boolean, default=False)
    created_at = db.Column(db.DateTime, nullable=False)
    updated_at = db.Column(db.DateTime, default=datetime.datetime.now(), nullable=False)


class Order_details(db.Model):
    __tablename__ = 'order_details'
    id = db.Column(db.Integer, primary_key=True, nullable=False, autoincrement=True)
    order_id = db.Column(db.Integer, db.ForeignKey("orders.id"), nullable=False)
    # order = db.relationship("Orders", backref="order_details")
    goods_id = db.Column(db.Integer, db.ForeignKey("goods.id"), nullable=False)
    good = db.relationship(Goods, backref="order_details")
    price = db.Column(db.DECIMAL(11, 2), nullable=False)
    num = db.Column(db.Integer, nullable=False)
    created_at = db.Column(db.DateTime, nullable=False)
    updated_at = db.Column(db.DateTime, default=datetime.datetime.now(), nullable=False)


class Orders(db.Model):
    __tablename__ = 'orders'
    id = db.Column(db.Integer, primary_key=True, nullable=False, autoincrement=True)
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)
    user = db.relationship("Users", backref="orders")
    order_num = db.Column(db.String(64), nullable=False)
    amount = db.Column(db.Integer, nullable=False)
    address_id = db.Column(db.Integer, db.ForeignKey("address.id"), nullable=False)
    # address = db.relationship("Address", backref="orders")
    status = db.Column(db.Integer, default=1)
    express_type = db.Column(db.String(16))
    express_num = db.Column(db.String(255))
    pay_time = db.Column(db.TIMESTAMP(True))
    pay_type = db.Column(db.String(16))
    trade_num = db.Column(db.String(255))
    order_details = db.relationship(Order_details, backref="order")
    created_at = db.Column(db.DateTime, nullable=False)
    updated_at = db.Column(db.DateTime, default=datetime.datetime.now(), nullable=False)


class Slides(db.Model):
    __tablename__ = 'slides'
    id = db.Column(db.Integer, primary_key=True, nullable=False, autoincrement=True)
    title = db.Column(db.String(255), nullable=False)
    img = db.Column(db.String(255), nullable=False)
    url = db.Column(db.String(255))
    status = db.Column(db.Integer, default=0)
    seq = db.Column(db.Integer, default=1)
    created_at = db.Column(db.DateTime, nullable=False)
    updated_at = db.Column(db.DateTime, default=datetime.datetime.now(), nullable=False)


class Users(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True, nullable=False, autoincrement=True)
    name = db.Column(db.String(64), nullable=False)
    email = db.Column(db.String(255), nullable=False)
    password = db.Column(db.String(255), nullable=False)
    phone = db.Column(db.String(64))
    avatar = db.Column(db.String(64))
    is_locked = db.Column(db.Integer, default=0)
    password_verified = db.Column(db.TIMESTAMP(True))
    remember_token = db.Column(db.String(255))
    created_at = db.Column(db.DateTime, nullable=False)
    updated_at = db.Column(db.DateTime, default=datetime.datetime.now(), nullable=False)
